home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / MiniPlayer3 / Mini Player3 Main.c < prev    next >
C/C++ Source or Header  |  1991-09-04  |  12KB  |  553 lines

  1. /**************************************************
  2. *
  3. * Sample Movie Player 3
  4. *
  5. *    This program demonstrates how to open a movie file 
  6. *    and play it in a window using the Movie Controller Thing
  7. *
  8. *    Sample.h has definitions and equates
  9. *    Main.c contains the general Mac application stuff
  10. *    Movie Stuff.c has most of the interesting movie code
  11. *
  12. *    Interesting things to look at:
  13. *    How to open and close movies
  14. *    Finding the and getting the Movie Controller Thing
  15. *    Calling the player thing from the event loop
  16. *    Not having to do much else since the Movie Controller does it all for you
  17. *
  18. *     Rich Williams 11/90, 2/91
  19. *    Updated to 4.04 interfaces
  20. *    Updated to MPW/Think 5.0/DSG interfaces 9/91 Chris Thorman
  21. *        
  22. ***************************************************/
  23.  
  24. #include "Movies.h"
  25. #include "Mini Player3.h"
  26.  
  27. #include <Memory.h>
  28. #include <Fonts.h>
  29. #include <Palettes.h>
  30. #include <OSEvents.h>
  31. #include <Menus.h>
  32. #include <Sound.h>
  33. #include <ToolUtils.h>
  34. #include <Desk.h>
  35. #include <Resources.h>
  36.  
  37. /**************************************************
  38. *
  39. * General Info
  40. *
  41. ***************************************************/
  42.  
  43. extern    numOpenMovies;
  44.  
  45. /**************************************************
  46. *
  47. *  various globals
  48. *
  49. ***************************************************/
  50.  
  51. extern    int    theModifiers;                    /* Used to keep the event modifiers */
  52. Rect        dragRect;
  53. MenuHandle    appleMenu, fileMenu, editMenu;
  54.  
  55. MovieInstance    movieList[maxMovies];        /* Array of open movies */
  56.  
  57.  
  58. /**************************************************
  59. ***************************************************
  60. *
  61. * Initialization Routines
  62. *
  63. * All of the stuff we call to get things started
  64. *
  65. ***************************************************
  66. ***************************************************/
  67.  
  68. /**************************************************
  69. *
  70. * InitMacintosh() Initialize all the managers & memory
  71. *
  72. ***************************************************/
  73. void InitMacintosh()
  74.  
  75. {
  76.     MaxApplZone();
  77.     InitGraf(&qd.thePort);
  78.     InitPalettes();
  79.     InitFonts();
  80.     FlushEvents(everyEvent, 0);
  81.     InitWindows();
  82.     InitMenus();
  83.     TEInit();
  84.     InitDialogs(0L);
  85.     InitCursor();
  86.  
  87. }
  88.  
  89. /**************************************************
  90. *
  91. * SetUpMenus()
  92. *
  93. * Reads in the menu resources and installs them
  94. *
  95. *
  96. ***************************************************/
  97. void SetUpMenus()
  98.  
  99. {
  100.     InsertMenu(appleMenu = GetMenu(appleID), 0);
  101.     InsertMenu(fileMenu = GetMenu(fileID), 0);
  102.     InsertMenu(editMenu = GetMenu(editID), 0);
  103.     DrawMenuBar();
  104.     AddResMenu(appleMenu, 'DRVR');
  105.     
  106. }
  107.  
  108. /**************************************************
  109. *
  110. * SetUpWindows()
  111. *
  112. * INits stuff for windows
  113. *
  114. ***************************************************/
  115. void SetUpWindows()
  116.  
  117. {    
  118.     /* Create the window for the picture */    
  119.     dragRect = qd.screenBits.bounds;
  120.  
  121. }
  122.  
  123.  
  124. /**************************************************
  125. ***************************************************
  126. *
  127. *    Routines for handling menus.
  128. *
  129. ***************************************************
  130. ***************************************************/
  131.  
  132. /**************************************************
  133. *
  134. *  AdjustMenus()
  135. *
  136. *    Enable or disable the items in the Edit menu if a DA window
  137. *    comes up or goes away. Our application doesn't do anything with
  138. *    the Edit menu.
  139. *
  140. ***************************************************/
  141. void AdjustMenus()
  142. {
  143.     register WindowPeek wp = (WindowPeek) FrontWindow();
  144.     short kind = wp ? wp->windowKind : 0;
  145.     Boolean DA = kind < 0;
  146.     
  147.     enable(editMenu, 1, DA);
  148.     enable(editMenu, 3, DA);
  149.     enable(editMenu, 4, DA);
  150.     enable(editMenu, 5, DA);
  151.     enable(editMenu, 6, DA);
  152.     
  153.     enable(fileMenu, openItem, numOpenMovies<maxMovies);
  154.     enable(fileMenu, closeItem, DA || (numOpenMovies > 0));
  155.  
  156. }
  157.  
  158. static
  159. void enable(MenuHandle menu, int item, Boolean ok)
  160. {
  161.     if (ok)
  162.         EnableItem(menu, item);
  163.     else
  164.         DisableItem(menu, item);
  165. }
  166.  
  167. /**************************************************
  168. *
  169. *  HandleMenu (mSelect)
  170. *
  171. *    Handle the menu selection. mSelect is what MenuSelect() and
  172. *    MenuKey() return: the high word is the menu ID, the low word
  173. *    is the menu item
  174. *
  175. ***************************************************/
  176. void HandleMenu (mSelect)
  177.  
  178. long    mSelect;
  179.  
  180. {
  181.     int            menuID = HiWord(mSelect);
  182.     int            menuItem = LoWord(mSelect);
  183.     Str255        name;
  184.     GrafPtr        savePort;
  185.     WindowPeek    frontWindow;
  186.     
  187.     switch (menuID)
  188.     {
  189.         case    appleID:                /* The Apple menu */
  190.             switch (menuItem)
  191.             {
  192.                 case    aboutItem:
  193.                     DoAboutBox();        /* Display the About Box */
  194.                     break;
  195.                     
  196.                 default:                /* It's a DA */
  197.                     GetPort(&savePort);
  198.                     GetItem(appleMenu, menuItem, name);
  199.                     OpenDeskAcc(name);
  200.                     SetPort(savePort);
  201.                     break;
  202.             }
  203.             break;
  204.     
  205.           case    fileID:                    /* The file menu */
  206.             switch (menuItem)
  207.               {
  208.                   case    openItem:
  209.                       DoOpen();            /* Open a movie */
  210.                     break;
  211.                                 
  212.                   case    closeItem:
  213.                     if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  214.                           break;
  215.               
  216.                     if (frontWindow->windowKind < 0)
  217.                           CloseDeskAcc(frontWindow->windowKind);
  218.                     else
  219.                         /* It's the movie window ( the only one we've got ) */
  220.                         /* Close it & clean up the movie stuff */
  221.                         CloseEm( (WindowPtr) frontWindow);
  222.  
  223.                     break;
  224.  
  225.                                                             
  226.                 case    quitItem:        /* Pack up and go home */
  227.                     CloseAllWindows();
  228.                     ExitToShell();
  229.                     break;
  230.             }
  231.             break;
  232.                   
  233.         case    editID:
  234.             if (!SystemEdit(menuItem-1))
  235.                   SysBeep(5);
  236.             break;
  237.       }
  238. }
  239.  
  240. /**************************************************
  241. *
  242. *  CloseEm(w:WindowPtr) Closes window w 
  243. *    if w is a movie window, clean up the movie and the player
  244. *
  245. ***************************************************/
  246. void CloseEm(w)
  247.  
  248. WindowPtr w;
  249.  
  250. {
  251.     MovieInstance    *theMovie;
  252.     
  253.     HideWindow(w);
  254.     theMovie = WhichMovieWindow(w);    /* Is it a movie window */
  255.     if (theMovie)    /* If its the movie window, throw out the movie */
  256.     {
  257.         CleanUpMovie(theMovie);
  258.     }
  259. }
  260.  
  261.  
  262. /**************************************************
  263. *
  264. *  CloseAllWindows() 
  265. *    Throws out all windows & movies
  266. *
  267. ***************************************************/
  268. void CloseAllWindows()
  269.  
  270. {
  271.     short    movieCount;
  272.     
  273.     /* Throw out all of the movies */
  274.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  275.         CleanUpMovie(&movieList[movieCount]);
  276.  
  277.     /* Put any other windows here */
  278. }
  279.  
  280. /**************************************************
  281. *
  282. *  DoAboutBox() Draws and displays the about box
  283. *
  284. ***************************************************/
  285. void DoAboutBox()
  286.  
  287. {
  288.     DialogPtr    theDialog;        /* Stuff for the about box */
  289.     short        itemHit, io;
  290.     Handle        tempHandle, soundHandle;
  291.     Rect        box;
  292.     
  293.     soundHandle = GetResource('snd ',meowID);
  294.     theDialog = GetNewDialog(aboutDlgID,0L,(WindowPtr)-1L);
  295.     
  296.     /* Highlight the default button */
  297.     SetPort(theDialog);
  298.     GetDItem(theDialog,1,&itemHit,&tempHandle,&box);
  299.     PenSize(3,3);
  300.     InsetRect(&box,-4,-4);
  301.     FrameRoundRect(&box,16,16);
  302.     
  303.     /* Wait until the button is pressed */
  304.     do
  305.         ModalDialog(0l,&itemHit);
  306.     while (itemHit != 1);
  307.     
  308.     /* Button pressed, now meow and go home */
  309.     io = SndPlay(0L,soundHandle,0);
  310.     ReleaseResource(soundHandle);
  311.     DisposDialog(theDialog);
  312. }
  313.  
  314. /**************************************************
  315. *
  316. * DoOpen()
  317. *
  318. *    Brings up standard file dialog
  319. *    and opens the movie if one has been selected
  320. *
  321. ***************************************************/
  322. void DoOpen()
  323.  
  324. {
  325.     StandardFileReply        sfr;        /* New-style SF reply */
  326.     SFTypeList    myTypeList = {'MooV'};
  327.     
  328.     StandardGetFilePreview(0, 2, myTypeList, &sfr);
  329.     if (sfr.sfGood)
  330.         OpenTheMovie(&sfr);    
  331.  
  332. }
  333.  
  334. /**************************************************
  335. ***************************************************
  336. *
  337. *        The window routines
  338. *
  339. ****************************************************
  340. ***************************************************/
  341.  
  342. /**************************************************
  343. *
  344. * WhichMovieWindow(w) 
  345. *    Checks if window is for a movie
  346. *     and returns a pointer to it
  347. *    or 0 if it isn't a movie's window
  348. *
  349. ***************************************************/
  350. MovieInstance* WhichMovieWindow(w)
  351.  
  352. WindowPtr    w;
  353.  
  354. {
  355.     short    movieCount;
  356.     
  357.     /* Loop through looking for a match */
  358.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  359.         if (w == movieList[movieCount].window)
  360.             break;
  361.             
  362.     if (movieCount<maxMovies)    /* Did we find one? */
  363.         return(&movieList[movieCount]);
  364.     
  365.     return(0L);                    /* Didn't find one */
  366. }
  367.         
  368. /**************************************************
  369. *
  370. * IsMyWindow(w):Boolean 
  371. *    Checks if w is a valid window and mine
  372. *
  373. ***************************************************/
  374. Boolean IsMyWindow(w)
  375.  
  376. WindowPtr    w;
  377.  
  378. {
  379.     return( (w != 0L) && 
  380.              WhichMovieWindow(w)  );
  381. }
  382.  
  383.     
  384. /**************************************************
  385. ***************************************************
  386. *
  387. * Event handling stuff
  388. *
  389. **************************************************** 
  390. ***************************************************/
  391.  
  392. /**************************************************
  393. *
  394. * HandleMouseDown (theEvent)
  395. *
  396. *    Take care of mouseDown events.
  397. *
  398. ***************************************************/
  399. void HandleMouseDown(theEvent)
  400.  
  401. EventRecord    *theEvent;
  402.  
  403. {
  404.     WindowPtr    theWindow;
  405.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  406.     
  407.     switch (windowCode)
  408.     {
  409.         case inSysWindow: 
  410.             SystemClick (theEvent, theWindow);
  411.             break;
  412.         
  413.           case inMenuBar:
  414.               AdjustMenus();
  415.             HandleMenu(MenuSelect(theEvent->where));
  416.             break;
  417.         
  418.           case inDrag:
  419.               if (IsMyWindow(theWindow))
  420.                   SelectWindow(theWindow);
  421.                     DragWindow(theWindow, theEvent->where, &dragRect);
  422.                 break;
  423.             
  424.           case inContent:
  425.               if (IsMyWindow(theWindow))
  426.                 {
  427.                     if (theWindow != FrontWindow())
  428.                       SelectWindow(theWindow);
  429.                     else
  430.                       MovieMouseDown(theWindow,theEvent->where,theEvent->modifiers); /* Track the mouse */
  431.                 }
  432.               break;
  433.           
  434.         case inGoAway:
  435.               if (IsMyWindow(theWindow) && TrackGoAway(theWindow, theEvent->where))
  436.                 CloseEm(theWindow);        /* Close the appropriate windows */
  437.             break;
  438.         
  439.         break;
  440.       }
  441. }
  442.  
  443.  
  444. /**************************************************
  445. *
  446. * HandleEvent()
  447. *
  448. *        The main event dispatcher. This routine should be called
  449. *        repeatedly (it  handles only one event).
  450. *
  451. ***************************************************/
  452. void HandleEvent()
  453.  
  454. {
  455.     int            ok;
  456.     EventRecord    theEvent;
  457.     WindowPtr    theWindow;
  458.     
  459.     MovieInstance    *theMovie;
  460.  
  461.     HiliteMenu(0);
  462.     SystemTask ();        /* Handle desk accessories */
  463.     
  464.     ok = GetNextEvent (everyEvent, &theEvent);
  465.     /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  466.     !
  467.     ! Don't miss this!
  468.     ! MoviesTask is called from the main event loop to 
  469.     ! give extra time to the active movie
  470.     ! this is optional since CheckMovieControllers will update
  471.     ! all of the movies.  It also doesn't work too well yet.
  472.     !
  473.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  474.     if(theEvent.what == nullEvent)
  475.             MyMoviesTask();
  476.     
  477.     /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  478.     !
  479.     ! CheckMovieControllers is called from the main event loop to 
  480.     ! see if the event is for the player
  481.     !
  482.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  483.     theModifiers = theEvent.modifiers;        /* We may need these in the filter */
  484.     if (CheckMovieControllers(&theEvent))
  485.         return;
  486.  
  487.     else if (ok)
  488.       switch (theEvent.what)
  489.       {        
  490.         case mouseDown:
  491.             HandleMouseDown(&theEvent);
  492.             break;
  493.             
  494.         case keyDown: 
  495.         case autoKey:
  496.             if ((theEvent.modifiers & cmdKey) != 0)
  497.             {
  498.               AdjustMenus();
  499.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  500.             }
  501.             break;
  502.             
  503.         case updateEvt:
  504.             /* To update the window, make the player replay the last frame */
  505.             theWindow = (WindowPtr) theEvent.message;    /* Get the window */
  506.  
  507.             theMovie = WhichMovieWindow(theWindow);            /* Is it a Movie window? */
  508.  
  509.             BeginUpdate(theWindow);            
  510.             if(theMovie)
  511.                 DoMovieUpdate(theMovie);
  512.             DrawControls(theWindow);
  513.             EndUpdate(theWindow);
  514.             break;
  515.             
  516.         case activateEvt:
  517.             theMovie = WhichMovieWindow((WindowPtr) theEvent.message);        /* Is it a Movie window? */
  518.             if (theMovie)
  519.             {
  520.                 if (theEvent.modifiers & activeFlag)
  521.                     DoMovieActivate(theMovie);
  522.                 else
  523.                     DoMovieDeactivate(theMovie);
  524.             }
  525.             else        /* Some other window, make it redraw */
  526.             {
  527.                 SetPort( (WindowPtr) theEvent.message );
  528.                 InvalRect(&((WindowPtr)theEvent.message)->portRect);    /* Force an update event */
  529.             }
  530.             
  531.             break;
  532.        }
  533. }
  534.  
  535. /**************************************************
  536. *
  537. * main()
  538. *
  539. *    This is where everything happens
  540. *
  541. ***************************************************/
  542. main()
  543.  
  544. {    
  545.     InitMacintosh();        /* Initialize everything */
  546.     SetUpMenus();
  547.     SetUpWindows();
  548.     SetUpMovies();
  549.  
  550.     for (;;)
  551.         HandleEvent();
  552. }
  553.